package com.luwanglin.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luwanglin.commonutils.JwtUtils;
import com.luwanglin.commonutils.MD5;
import com.luwanglin.educenter.entity.UcenterMember;
import com.luwanglin.educenter.entity.vo.LoginInfoVo;
import com.luwanglin.educenter.entity.vo.LoginVo;
import com.luwanglin.educenter.entity.vo.RegisterVo;
import com.luwanglin.educenter.mapper.UcenterMemberMapper;
import com.luwanglin.educenter.service.UcenterMemberService;
import com.luwanglin.commonutils.exception.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author lwl
 * @since 2021-05-23
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Autowired
    private UcenterMemberMapper mapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public String login(LoginVo loginVo) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
        if (org.apache.commons.lang3.StringUtils.isAnyEmpty(mobile, password)) {
            throw new GuliException(20001, "login failed");
        }
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        UcenterMember member = mapper.selectOne(wrapper);
        if (StringUtils.isEmpty(member)) {
            throw new GuliException(20001, "login failed");
        }
        if (!MD5.encrypt(password).equals(member.getPassword())) {
            throw new GuliException(20001, "login failed");
        }
        if (member.getIsDisabled()) {
            throw new GuliException(20001, "login failed");
        }
        return JwtUtils.getJwtToken(member.getId(), member.getNickname());
    }

    @Override
    public void register(RegisterVo registerVo) {
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        if (org.apache.commons.lang3.StringUtils.isAnyEmpty(code, mobile, nickname, password)) {
            throw new GuliException(20001, "register failed");
        }
        String codeRedis = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(codeRedis)) {
            throw new GuliException(20001, "register code invalidation !");
        }
        QueryWrapper<UcenterMember> ucenterMemberQueryWrapper = new QueryWrapper<>();
        ucenterMemberQueryWrapper.eq("mobile", mobile);
        Integer integer = mapper.selectCount(ucenterMemberQueryWrapper);
        if (integer > 0) {
            throw new GuliException(20001, "register account already exist! ");
        }
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile(mobile);
        ucenterMember.setNickname(nickname);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setIsDisabled(false);
        ucenterMember.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        mapper.insert(ucenterMember);
    }

    @Override
    public LoginInfoVo getLoginInfo(String memberId) {
        UcenterMember ucenterMember = mapper.selectById(memberId);
        LoginInfoVo loginInfoVo = new LoginInfoVo();
        BeanUtils.copyProperties(ucenterMember, loginInfoVo);
        return loginInfoVo;
    }

    @Override
    public UcenterMember getByOpenid(String openid) {
        QueryWrapper<UcenterMember> ucenterMemberQueryWrapper = new QueryWrapper<>();
        ucenterMemberQueryWrapper.eq("openid", openid);
        return mapper.selectOne(ucenterMemberQueryWrapper);
    }
    @Override
    public Integer countRegisterByDay(String day) {
        return baseMapper.selectRegisterCount(day);
    }
}
